/* 击杀反馈
Author : GEORGE FLOROS [GR]  22/03/2018
k1升级优化 2021.12.9 游戏版本需1.56以上

Headshot + Killfeed Script
https://forums.bistudio.com/forums/topic/206593-headshot-killfeed-script/
http://www.armaholic.com/page.php?id=32989
*/

if (isDedicated || {!isNil "Killfeed_onMouseMove"}) exitWith {};

Killfeed_onMouseMove = {
	Killfeed_safeZoneX = Killfeed_safeZoneX + ((_this select 1) / 100);
	if (Killfeed_safeZoneX < -.999) then {Killfeed_safeZoneX = -.999};
	if (Killfeed_safeZoneX > 1) then {Killfeed_safeZoneX = 1};
	Killfeed_safeZoneY = Killfeed_safeZoneY + ((_this select 2) / 100);
	if (Killfeed_safeZoneY < 0) then {Killfeed_safeZoneY = 0};
	if (Killfeed_safeZoneY > 1) then {Killfeed_safeZoneY = 1};
};
Killfeed_onMouseWheel = {
	Killfeed_img_size = if((_this select 1) > 0)then[{Killfeed_img_size + .1},{Killfeed_img_size - .1}];
	if (Killfeed_img_size < 0) then {Killfeed_img_size = 0};
	if (Killfeed_img_size > 10) then {Killfeed_img_size = 10};
};
Killfeed_safeZoneXY_Text = {
	if (isNil "HEADSHOT" || {isNil "Killfeed_safeZoneX"} || {isNil "Killfeed_safeZoneY"} || {isNil "Killfeed_align"} || {isNil "Killfeed_br"}) exitWith {};
	Killfeed_safeZoneX = round(Killfeed_safeZoneX * 1000) / 1000;
	Killfeed_safeZoneY = round(Killfeed_safeZoneY * 1000) / 1000;
	cutText [format["X轴: %1\nY轴: %2", Killfeed_safeZoneX,Killfeed_safeZoneY], "Plain Down"];
	[format["<t color='#fff000' size = '.6' align='%1'>====================%2击杀反馈文本显示位置%2====================</t>",Killfeed_align,Killfeed_br],Killfeed_safeZoneX,Killfeed_safeZoneY * safeZoneH + safeZoneY,10,0,0,7018] spawn BIS_fnc_dynamicText;
};
Killfeed_imgsize_Text = {
	if (isNil "HEADSHOT" || {isNil "Killfeed_img_size"}) exitWith {};
	Killfeed_img_size = round(Killfeed_img_size * 10) / 10;
	cutText [format["图标尺寸: %1", Killfeed_img_size], "Plain Down"];
	_picture = getText (configFile >> "cfgWeapons" >> "arifle_MX_GL_Hamr_pointer_F" >> "picture");
	[format["<img size='%1' shadow='%2' image='%3'/>",Killfeed_img_size,Killfeed_img_shadow,_picture],-1,0 * safeZoneH + safeZoneY,10,0,0,7019] spawn BIS_fnc_dynamicText;
};
if (!isNil "CBA_Settings_fnc_init") then {
	["Killfeed_safeZoneX","SLIDER",["显示位置: 水平X轴"],"击杀反馈设置",[-1,1,safeZoneX,3],false,{call Killfeed_safeZoneXY_Text}] call CBA_Settings_fnc_init;
	["Killfeed_safeZoneY","SLIDER",["显示位置: 垂直Y轴"],"击杀反馈设置",[0,1,0,3],false,{call Killfeed_safeZoneXY_Text}] call CBA_Settings_fnc_init;
	["Killfeed_align","LIST",["对齐方式"],"击杀反馈设置",[["left","center","right"],["左对齐（默认）","居中","右对齐"],0],false,{call Killfeed_safeZoneXY_Text}] call CBA_Settings_fnc_init;
	["Killfeed_br","LIST",["显示方式"],"击杀反馈设置",[["","<br/>"],["单行显示（默认）","三行显示"],0],false,{call Killfeed_safeZoneXY_Text}] call CBA_Settings_fnc_init;
	["Killfeed_img_size","SLIDER",["图标尺寸"],"击杀反馈设置",[0,10,1,1],false,{call Killfeed_imgsize_Text}] call CBA_Settings_fnc_init;
	["Killfeed_img_shadow","LIST",["图标阴影"],"击杀反馈设置",[[0,1],["阴影关","阴影开（默认）"],1],false,{call Killfeed_imgsize_Text}] call CBA_Settings_fnc_init;
};

uiSleep 20;
if (!isNil "KILLlist") then {removeAllMissionEventHandlers "EntityKilled"}; ////
sleep .3;
if (!isNil "HEADSHOT") then {HEADSHOT_VAR = 1};
if (isNil "headshot_script") then {headshot_script = {}};

if(isNil "Killfeed_safeZoneX")then{Killfeed_safeZoneX = 0};
if(isNil "Killfeed_safeZoneY")then{Killfeed_safeZoneY = 0};
if(isNil "Killfeed_align")then{Killfeed_align = "left"};
if(isNil "Killfeed_br")then{Killfeed_br = ""};
if(isNil "Killfeed_img_size")then{Killfeed_img_size = 1};
if(isNil "Killfeed_img_shadow")then{Killfeed_img_shadow = 1};
player createDiarySubject ["Killfeed_set", "击杀反馈"];
player createDiaryRecord ["Killfeed_set", ["文本显示设置", "<br/>k1升级优化 2021.12.9<br/><br/>按Esc键在CBA提供的插件选项中可以保存设置<br/>这里也可以用鼠标进行更快捷的设置<br/>鼠标滚轮用于缩放图标尺寸需关闭地图（默认1）<br/><execute expression = 'if (isNil ""Killfeed_KH"") then {Killfeed_KH = (findDisplay 46) displayAddEventHandler [""KeyDown"", {if(_this select 1 == 45)then{{(findDisplay 46) displayRemoveEventHandler _x} forEach [[""KeyDown"",Killfeed_KH],[""MouseMoving"",Killfeed_MMH],[""MouseZChanged"",Killfeed_MZH]]; Killfeed_KH = nil; systemchat ""击杀反馈: 退出设置""}}]; Killfeed_MMH = (findDisplay 46) displayAddEventHandler [""MouseMoving"", {call Killfeed_onMouseMove; call Killfeed_safeZoneXY_Text}]; Killfeed_MZH = (findDisplay 46) displayAddEventHandler [""MouseZChanged"", {call Killfeed_onMouseWheel; call Killfeed_imgsize_Text}]; systemchat ""通过移动鼠标改变显示位置""};'>用鼠标设置（X键退出）</execute>  <execute expression = 'if(!isNil ""Killfeed_KH"")then{{(findDisplay 46) displayRemoveEventHandler _x} forEach [[""KeyDown"",Killfeed_KH],[""MouseMoving"",Killfeed_MMH],[""MouseZChanged"",Killfeed_MZH]]; Killfeed_KH = nil; systemchat ""击杀反馈: 退出设置""}'>取消</execute><br/><br/>对齐方式<br/><execute expression = 'Killfeed_align = ""left""; call Killfeed_safeZoneXY_Text; systemchat ""左对齐（默认）"";'>左对齐</execute>  <execute expression = 'Killfeed_align = ""center""; call Killfeed_safeZoneXY_Text; systemchat ""居中"";'>居中</execute>  <execute expression = 'Killfeed_align = ""right""; call Killfeed_safeZoneXY_Text; systemchat ""右对齐"";'>右对齐</execute><br/><br/>显示方式<br/><execute expression = 'Killfeed_br = """"; call Killfeed_safeZoneXY_Text; systemchat ""单行显示"";'>单行显示（默认）</execute>  <execute expression = 'Killfeed_br = ""<br/>""; call Killfeed_safeZoneXY_Text; systemchat ""三行显示"";'>三行显示</execute><br/><br/>图标阴影<br/><execute expression = 'Killfeed_img_shadow = 1; call Killfeed_imgsize_Text; systemchat ""阴影开"";'>阴影开（默认）</execute>  <execute expression = 'Killfeed_img_shadow = 0; call Killfeed_imgsize_Text; systemchat ""阴影关"";'>阴影关</execute><br/>"]];

Killfeed_GFText1 = {
	params ["_picture","_killer_Color","_vehName","_player_name","_Head","_killed_Color","_killed_Name","_distance","_use","_weapon"];
	
	systemChat format["%1 %2击杀 %3 距离 %4 m%5%6",_player_name,(if(_Head)then[{"@"},{""}]),_killed_Name,_distance,_use,_weapon];
	
	_killHead = if (_Head) then [{"<img size='.5' image='a3\ui_f\data\IGUI\Cfg\simpleTasks\types\kill_ca.paa' color='#FFFFFF'/>"},{""}];
	
	if (Killfeed_br == "") then [{
		[format["<t size='.6' align='%11'><img size='%12' shadow='%13' image='%1'/><t color='%2'> [%3] %4 %5<t color='#FFFFFF'>击杀 <t color='%6'>%7<t color='#FFFFFF'> 距离 <t color='#FFD700'>%8 m<t color='#FFFFFF'>%9<t color='#FF0000'>%10</t>",_picture,_killer_Color,_vehName,_player_name,_killHead,_killed_Color,_killed_Name,_distance,_use,_weapon,Killfeed_align,Killfeed_img_size,Killfeed_img_shadow],Killfeed_safeZoneX,Killfeed_safeZoneY * safeZoneH + safeZoneY,10,0,0,7016] spawn BIS_fnc_dynamicText;
	},{
		[format["<t size='.6' align='%11'><t color='%2'>[%3] %4 %5<t color='#FFFFFF'>击杀 <t color='%6'>%7<t color='#FFFFFF'> %12距离 <t color='#FFD700'>%8 m<t color='#FFFFFF'>%9<t color='#FF0000'>%10</t>%12<img size='%13' shadow='%14' image='%1'/>",_picture,_killer_Color,_vehName,_player_name,_killHead,_killed_Color,_killed_Name,_distance,_use,_weapon,Killfeed_align,Killfeed_br,Killfeed_img_size,Killfeed_img_shadow],Killfeed_safeZoneX,Killfeed_safeZoneY * safeZoneH + safeZoneY,10,0,0,7016] spawn BIS_fnc_dynamicText;
	}];
};
Killfeed_sideColor = { //单位阵营默认HTML颜色代码
	if (_this isEqualTo west) exitWith {"#004C99"}; //蓝
	if (_this isEqualTo east) exitWith {"#800000"}; //红
	if (_this isEqualTo resistance) exitWith {"#008000"}; //绿
	if (_this isEqualTo civilian) exitWith {"#660080"}; //紫
	if (_this isEqualTo sideEnemy) exitWith {"#663380"}; //浅紫
	"#B29900" //深黄
};
k1_allUnits = {
	private ["_objs","_obj"];
	_objs = [];
	{
		_obj = agent _x;
		if (alive _obj) then {_objs pushback _obj};
	} forEach agents;
	_objs append allUnits;
	_objs
}; ////
kk_vehRole = {
	private ["_role","_turretPath","_cargoIndex"];
	_role = "";
	_turretPath = [];
	_cargoIndex = 0;
	{if (_x select 0 == _this) exitWith {_role = toLower (_x select 1); _turretPath = _x select 3; _cargoIndex = _x select 2}} forEach fullCrew vehicle _this;
	[_role,_turretPath,_cargoIndex]
};
HEADSHOT = {
	_id = _this addEventHandler ["HitPart", {
		_arr = _this select 0;
		_target = _arr select 0;
		_shooter = _arr select 1;
		if (_shooter == player) then {
			_head = ("head" in (_arr select 5));
			_target setVariable ["Headshot_killHead", _head];
			_target setVariable ["Killfeed_AMMO", (_arr select 6) select 4];
		}; //包含载具击杀
	}];
	_this setVariable ["Headshot_GF_eh", _id];
}; //冲击波杀死不触发
addMissionEventHandler ["EntityKilled", {
	_this spawn {
		private ["_weapon", "_picture", "_vehName", "_curWep2"];
		params ["_killed", "_killer", "_instigator"];
		//爆头相关
		if (_killed getVariable ["EΗ_HEADSHOT",false]) then {
			_killed setVariable ["EΗ_HEADSHOT",nil];
			_killed removeEventHandler ["HitPart", (_killed getVariable ["Headshot_GF_eh", 0])];
		};
		//_killer会返回载具有时里面的玩家一起触发! 副驾驶击杀无法判断
		if ((_killer == player || {_instigator == player} || {vehicle _killer == getConnectedUAV player} || {_killer == vehicle player && {isNull _instigator} && {(player call kk_vehRole) select 0 != "cargo"}}) && {_killed isKindOf "CAManBase"} && {_killed != player}) then [{
			if (isNull _killer) then {_killer = vehicle _instigator}; //
			
			_killed_Name = if(isPlayer _killed)then[{name _killed},{getText(configFile >> "CfgVehicles" >> format["%1",typeOf _killed] >> "Displayname")}];
			
			_distance = floor(_killer distance2D _killed);
			//_killed_Color = (side group _killed call BIS_fnc_sideColor) call BIS_fnc_colorRGBtoHTML;
			//_killer_Color = (side group player call BIS_fnc_sideColor) call BIS_fnc_colorRGBtoHTML;
			_killed_Color = (side group _killed) call Killfeed_sideColor;
			_killer_Color = (side group player) call Killfeed_sideColor;
			
			[format["<t size='0.5'>击杀<t size='0.8' color='%1'>  %2<t color='#FFD700'>  %3 m</t>",_killed_Color,_killed_Name,_distance],0,.356 * safeZoneH + safeZoneY,2,0,0,7017] spawn BIS_fnc_dynamicText;
			
			if(!isNil "HEADSHOT_VAR" || {!isNil "KILLHINT_SOUND"})then{playSound "Killfeed_notification"};
			
			sleep .1; //.3
			
			_mag = "";
			_ammo = _killed getVariable ["Killfeed_AMMO", ""];
			_killed setVariable ["Killfeed_AMMO", nil];
			{if (_ammo in _x) exitWith {_mag = _x select 1}} forEach Killfeed_magazine_Indexes;
			
			_Head = if(_mag == "" && {_killed getVariable ["Headshot_killHead",false]} && {getText(configFile >> "CfgAmmo" >> _ammo >> "simulation") == "shotBullet"})then[{true},{false}];
			_killed setVariable ["Headshot_killHead", nil];
			if (_Head) then {
				_killed spawn headshot_script;
				[parseText format ["<t align='center' font='PuristaBold' size='2.8'><img size='2' image='a3\ui_f\data\IGUI\Cfg\simpleTasks\types\kill_ca.paa'/>爆头击杀</t>"], true, nil,  1, .7, 0] spawn BIS_fnc_textTiles;
				if (!isNil "HEADSHOT_VAR" || {!isNil "KILLHINT_SOUND"}) then {
					_sound = selectRandom ["HEADSHOT_Blow_it_out_your_ass","HEADSHOT_Come_get_some","HEADSHOT_Come_on","HEADSHOT_Damn_i_m_good","HEADSHOT_Eat_shit_and_die","HEADSHOT_Game_over","HEADSHOT_Gotta_hurt","HEADSHOT_Holy_shit","HEADSHOT_It_hurts_to_be_you","HEADSHOT_lets_rock","HEADSHOT_Make_day","HEADSHOT_Mess","HEADSHOT_Ooooh_thats_gotta_hurt","HEADSHOT_See_you_in_hell","HEADSHOT_Shake_it_baby","HEADSHOT_Shit_happens","HEADSHOT_Terminated","HEADSHOT_Ugly","HEADSHOT_Wasted","HEADSHOT_Yippie_kaiay"];
					player say3D _sound;
					Killfeed_say3D = [player,_sound];
					publicVariable "Killfeed_say3D";
				};
			};
			
			_curWep2 = false;
			_vehName = getText (configFile >> "cfgVehicles" >> typeOf player >> "displayname");
			if (_mag != "") then [{
				_weapon = getText (configFile >> "cfgMagazines" >> _mag >> "displayname");
				_picture = getText (configFile >> "cfgMagazines" >> _mag >> "picture");
			},{
				_veh = vehicle _killer;
				_curWep = if (_killer == _instigator) then [{currentWeapon _killer},{
					if (isNull _instigator) exitWith {currentWeapon _veh}; //
					//_role = assignedVehicleRole _instigator; //用下面code替换
					_vehRole = _instigator call kk_vehRole;
					_role = _vehRole select 0;
					_turretPath = _vehRole select 1;
					_cargoIndex = _vehRole select 2;
					if (_role in ["turret","gunner","commander"] && {_cargoIndex == -1} && {count _turretPath > 0}) then [{_turretWep = _veh currentWeaponTurret _turretPath; if (_turretWep == "") then [{currentWeapon _veh},{_turretWep}]},{currentWeapon _veh}]; //支持多炮塔
				}];
				_weapon = getText (configFile >> "CfgWeapons" >> _curWep >> "displayname");
				
				if (_killer != _instigator) then {
					_vehName = getText (configFile >> "cfgVehicles" >> typeOf _veh >> "displayname");
					if(!(_veh isKindOf "man") && {(isNull _instigator && {_killer isKindOf "man"}) || {_weapon in ["","喇叭","雷射标记","激光指向标","Horn","Laser Marker"]}})then{_weapon = _vehName};
				};
			
				_curWep2 = _curWep in ["Binocular","Rangefinder","Laserdesignator_01_khk_F","Laserdesignator_02","Laserdesignator_03","Laserdesignator","Laserdesignator_02_ghex_F","rhssaf_zrak_rd7j","rhsusf_lrf_Vector21","rhs_tr8_periscope_pip","rhs_tr8_periscope","rhs_pdu4","Leupold_Mk4","lerca_1200_tan","lerca_1200_black"];
				
				_picture = getText (configFile >> "cfgWeapons" >> _curWep >> "picture");
				if (isNil "_picture" || {_picture == ""} || {_curWep2}) then {
					_picture = getText (configFile >> "cfgVehicles" >> typeOf _veh >> "picture");
				};
			}];
			
			_use = if(isNil "_weapon" || {_weapon == ""} || {_curWep2})then[{_weapon = ""; _picture = ""; ""},{" 使用 "}];
			Killfeed_arr = [_picture,_killer_Color,_vehName,name player,_Head,_killed_Color,_killed_Name,_distance,_use,_weapon];
			publicVariable "Killfeed_arr";
			Killfeed_arr call Killfeed_GFText1;
		},{
			_killed setVariable ["Headshot_killHead",nil];
			_killed setVariable ["Killfeed_AMMO", nil];
		}];
	};
}];

"Killfeed_arr" addPublicVariableEventHandler {(_this select 1) call Killfeed_GFText1};
"Killfeed_say3D" addPublicVariableEventHandler {(_this select 1) params ["_ply","_sound"]; _ply say3D _sound};

if (!isNil "HEADSHOT_VAR") exitWith {
	{
		_x removeAllEventHandlers "HitPart";
		_x setVariable ["EΗ_HEADSHOT",nil];
		
		_x setVariable ["AC_EΗ_HitPart",nil]; //AC
	} forEach (call k1_allUnits); //allUnits
};

Killfeed_magazine_Indexes = [["APERSMineDispenser_Mine_Ammo","APERSMineDispenser_Mag"]]; //

systemChat "添加击杀反馈";

while {true} do {
	{	
		if (!(_x getVariable ["EΗ_HEADSHOT",false]) && {alive _x} && {_x isKindOf "CaManBase"}) then {
			_x spawn HEADSHOT;				
			_x setVariable ["EΗ_HEADSHOT",true]; //由原版决定
	 	};
	} forEach (call k1_allUnits); //allUnits
	
	if (player getVariable ["Killfeed_Fired_EΗ",true] && {alive player}) then {
		player setVariable ["Killfeed_Fired_EΗ",false];
		player addEventHandler ["Fired", {
			if (count _this > 5) then {
				_wep = _this select 1;
				_ammo = _this select 4;
				_mag = _this select 5;
				if ((_wep == "Throw" || {_wep == "Put"}) && {!([_ammo,_mag] in Killfeed_magazine_Indexes)}) then {Killfeed_magazine_Indexes pushback [_ammo,_mag]};
			};
		}];
	};
	sleep 3; //
};


